import React, { Component } from 'react';
import { Upload, message, Button, Col, Input, Select } from 'antd';
import { userStore } from '@/stores/user';
import { observer } from 'mobx-react';
import { useState } from "react";
import styles from './index.less';
import { LoadingOutlined, PlusOutlined, ForwardFilled } from '@ant-design/icons';
import Header from '@/pages/components/header';
import Footer from '@/pages/components/footer';
import SingUpService from '@/services/signUpService';
import SignUpModel from '@/models/signUp';
import SingUpStore, { signUpStore } from '@/stores/signUpStore';

const { Option } = Select;
const { TextArea } = Input;

function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

function beforeUpload(file) {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('You can only upload JPG/PNG file!');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('Image must smaller than 2MB!');
  }
  return isJpgOrPng && isLt2M;
}

@observer
export default class SignUp extends Component {
  // componentDidMount() {
  //   signUpStore.saveSignUp()
  // }
  state = {
    loading: false,
    imageUrl: '',
  };

  handleChange = info => {
    if (info.file.status === 'uploading') {
      this.setState({ loading: true });
      return;
    }
    if (info.file.status === 'done') {
      // Get this url from response in real world.
      getBase64(info.file.originFileObj, imageUrl =>
        this.setState({
          imageUrl,
          loading: false,
        }),
      );
    }
  };

  inputChange(e: any) {
    let key = e.target.name;
    let value = e.target.value;
    console.log(value);
    switch (key) {
      case 'username':
        signUpStore.username = value;
        break;
      case 'email':
        signUpStore.email = value;
        break;
      case 'content':
        signUpStore.content = value;
        break;
      case 'cover':
        signUpStore.cover = value;
        break;
      case 'phone':
        signUpStore.pnone = value;
        break;
      case 'wokekName':
        signUpStore.workName = value;
        break;
      case 'gameType':
        signUpStore.gameType = value;
        break;
      case 'tag':
        signUpStore.tag = value;
        break;
      default:
        break;
    }
  }
  selectTag(e: any, option: any) {
    signUpStore.tag = e;
    signUpStore.tagName = option.children
  }
  selectField(e: any, option: any) {
    signUpStore.gameType = e;
    signUpStore.gameTypeName = option.children
  }
  render() {
    const { loading, imageUrl } = this.state;
    const uploadButton = (
      <div>
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div style={{ marginTop: 8 }}>Upload</div>
      </div>
    );

    return (
      <div>
        <Header />
        <div className={styles.info_box}>
          <div className={styles.sign_title}>报名信息</div>
          <div className={styles.now_devider}></div>
          <div className={styles.sign_items_box}>
            <div className={styles.sign_items}>姓名:</div>
            <Input placeholder="请输入您的姓名" name="username" onChange={this.inputChange} />
            <div className={styles.sign_items} >手机号:</div>
            <Input placeholder="请输入您的手机号" name="phone" onChange={this.inputChange} />
            <div className={styles.sign_items}>邮箱:</div>
            <Input placeholder="请输入您的邮箱" name="email" onChange={this.inputChange} />
            <div className={styles.sign_items}>作品名称:</div>
            <Input placeholder="请输入您的作品名称" name="workName" onChange={this.inputChange} />
            <div className={styles.sign_items}>作品类别:</div>
            <Select placeholder='请选择作品类别' allowClear className={styles.select}
              onSelect={(value, option) => this.selectTag(value, option)}
              value={signUpStore.tagName}
            >
              <Option value="1">国画</Option>
              <Option value="2">水墨画</Option>
              <Option value="3">素描</Option>
              <Option value="4">油画</Option>
              <Option value="5">漫画</Option>
              <Option value="6">简笔画</Option>
              <Option value="7">风景画</Option>

            </Select>
            {/* <div className={styles.sign_items}>作品主题:</div> */}

            <div className={styles.sign_items}>作品:</div>
            <Upload
              name="avatar"
              listType="picture-card"
              className="avatar-uploader"
              showUploadList={false}
              // action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
              beforeUpload={beforeUpload}
              onChange={this.handleChange}
            >
              {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
            </Upload>
            <div className={styles.sign_items} >简介:</div>
            <TextArea rows={10} placeholder="不少于200字" maxLength={500} name="content" onChange={this.inputChange} />
            <Button className={styles.sign_submit} onClick={() => signUpStore.saveSignUp()}>提交</Button>
          </div>
        </div>
        <Footer />
      </div>
    )
  }
}